#ifndef templateMerger_h
#define templateMerger_h

#include "TObjString.h"
#include "TList.h"
#include "TMath.h"
#include "TLegend.h"
#include "TGraph.h"
#include "TArrayD.h"
#include "TAxis.h"
#include "TH1.h"

/**
	templateMerger -- makes Add all _daughters to _mother and renormalize to 1 obtained _mother


	_mode == mode of normalization -- 0 normalized to 1, 1 -- not normalized

**/


bool templateMerger(TH1 * & _mother, TList * _daughters, int _mode = 1)
{

if (!(_mother && _daughters) || _daughters->IsEmpty()) {std::cout<<"Something wrong with templateMerger, return"<<std::endl; return false;}

double _motherInt = _mother->Integral();

//if (abs(_motherInt-1e0)>1e-6) { std::cout<<"templateMerger :: template is not normalized to 1, return"<<std::endl; return false;}

if (_mode==0) _mother->Scale(1e0/_motherInt);

for (int i=0;i<_daughters->GetSize();i++)
{

TH1 * _daughter = (TH1*)_daughters->At(i);
double _daughterInt = _daughter->Integral();
//if (abs(_daughterInt-1e0)>1e-6) { std::cout<<"templateMerger :: template is not normalized to 1, return"<<std::endl; return false;}	
if (_mode==0)  _daughter->Scale(1e0/_daughterInt);
_mother->Add(_daughter);

}

 _motherInt = _mother->Integral();
if (_mode ==0) _mother->Scale(1e0/_motherInt);

return true;
}


#endif
